package com.yuexun.beilunpatient.ui.inspect.ui;


import android.app.Dialog;
import android.content.Intent;
import android.text.Html;
import android.view.View;
import android.widget.TextView;

import com.yuexun.beilunpatient.R;
import com.yuexun.beilunpatient.base.BaseEntity;
import com.yuexun.beilunpatient.base.BaseKJActivity;
import com.yuexun.beilunpatient.config.AppConfig;
import com.yuexun.beilunpatient.ui.contractmanage.bean.VideoBean;
import com.yuexun.beilunpatient.ui.contractmanage.bean.VideoListBean;
import com.yuexun.beilunpatient.ui.contractmanage.model.impl.ContractManageModel;
import com.yuexun.beilunpatient.ui.contractmanage.presenter.impl.VideoPresenter;
import com.yuexun.beilunpatient.ui.contractmanage.ui.view.IVideoView;
import com.yuexun.beilunpatient.ui.inspect.bean.CheckDtlRow;
import com.yuexun.beilunpatient.ui.inspect.bean.CheckDtlRowSet;
import com.yuexun.beilunpatient.ui.inspect.model.impl.InspectDetailModel;
import com.yuexun.beilunpatient.ui.inspect.presenter.impl.InspectDetailPresenter;
import com.yuexun.beilunpatient.ui.inspect.presenter.impl.InspectWeblPresenter;
import com.yuexun.beilunpatient.ui.inspect.ui.view.IInspectDetailView;
import com.yuexun.beilunpatient.ui.inspect.ui.view.IInspectWeblView;
import com.yuexun.beilunpatient.ui.main.ui.Act_Webview;
import com.yuexun.beilunpatient.ui.main.ui.Act_X5Webview;
import com.yuexun.beilunpatient.widget.LoadingDialog;

import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;
import org.kymjs.kjframe.utils.SystemTool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by chenss on 2018/4/3 0003.
 */

public class Act_Inspect_Detail extends BaseKJActivity implements IInspectDetailView,IVideoView {

    @Bind(R.id.tv_content)
    TextView tvContent;
    @Bind(R.id.tv_judge)
    TextView tvJudge;
    @Bind(R.id.tv_reportno)
    TextView tvReportno;

    //申请单号
    private String applyNo;
    private String hospId;
    private String regTime;

    private InspectDetailPresenter presenter;
    VideoPresenter videoPresenter;
    private int nowpage = 1;
    private int pagesize = 20;

    CheckDtlRow checkDetailResp = new CheckDtlRow();

    LoadingDialog loadingDialog;

    private int type; //1:报告,2:影像

    @Override
    public void setRootView() {
        setContentView(R.layout.act_inspect_dtl);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        super.initData();
        loadingDialog = new LoadingDialog(this);
        presenter = new InspectDetailPresenter(this, new InspectDetailModel());
        videoPresenter = new VideoPresenter(this,new ContractManageModel());
        applyNo = getIntent().getExtras().getString("applyNo");
        hospId = getIntent().getExtras().getString("hospId");
        regTime = getIntent().getExtras().getString("regTime");

        if (SystemTool.checkNet(aty)) {
            loadingDialog.show();
            Map<String, String> params = new HashMap<>();
            params.put("applyNo", applyNo);
            params.put("hospId", hospId);
            params.put("sessionUuidPatient", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
            presenter.inquireCheckDetail(params);
        } else {
            ViewInject.toast("网络异常，请检查网络是否连接!");
        }
    }

    /**
     * 请请求数据-云影像列表
     */
    private void ApiGetData() {
        Map<String, String> params = new HashMap<>();
        params.put("sessionUuidPatient", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
        params.put("PageIndex", nowpage + "");
        params.put("PageSize", pagesize + "");
        params.put("regTimeStart", regTime);
        params.put("regTimeEnd", regTime);
        videoPresenter.cIcheckList(params);
    }

    /**
     * 请请求数据-云影像详情
     */
    private void ApiGetDataDetail(String observationUID) {
        Map<String, String> params = new HashMap<>();
        params.put("sessionUuidPatient", PreferenceHelper.readString(this, AppConfig.USER_INFO, AppConfig.SESSION_ID));
        params.put("observationUID", observationUID);
        videoPresenter.cIcheckInfo(params);
    }

    @OnClick({R.id.iv_back, R.id.ll_video, R.id.ll_report})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.ll_video:
                type =2;
                loadingDialog.show();
                ApiGetData();
                break;
            case R.id.ll_report:
                type =1;
                loadingDialog.show();
                ApiGetData();
                break;
        }
    }



    @Override
    public void showInspectDetail(ArrayList<CheckDtlRowSet> list) {
        if (list.get(0).getCode() == 1) {
            checkDetailResp = list.get(0).getRows().get(0);
            if (checkDetailResp.getImage_pres() != null) {
                tvContent.setText(Html.fromHtml(checkDetailResp.getImage_pres().replace("\n", "<br>")));
            }
            if (checkDetailResp.getImage_result() != null) {
                tvJudge.setText(Html.fromHtml(checkDetailResp.getImage_result().replace("\n", "<br>")));
            }
            tvReportno.setText("单号:" + applyNo);
        }

    }

    @Override
    public void dismiss() {
        loadingDialog.dismiss();
    }

    @Override
    public void cIcheckList(VideoListBean videoListBean) {
        if (videoListBean.getStatus().equals("200")) {
            if (videoListBean.getDatas().get(0).getData() != null) {
                if (videoListBean.getDatas().get(0).getData().getDataList().size() == 0) {
                    if(type == 1)
                    {
                        ViewInject.toast("报告不存在");
                    }
                    else {
                        ViewInject.toast("影像不存在");
                    }
                } else {
                  for(int i = 0 ;i<videoListBean.getDatas().get(0).getData().getDataList().size();i++)
                  {
                      if(checkDetailResp.getItem_name().equals(videoListBean.getDatas().get(0).getData().getDataList().get(i).getProcedureName()))
                      {
                          ApiGetDataDetail(videoListBean.getDatas().get(0).getData().getDataList().get(i).getObservationUID());
                      }
                  }
                }

            } else {
                ViewInject.toast(videoListBean.getDatas().get(0).getMsg());
            }
        } else {
            ViewInject.toast(videoListBean.getMessage());
        }
    }

    @Override
    public void cIcheckInfo(VideoBean videoBean) {
        if(videoBean.getStatus().equals("200"))
        {
            tvContent.setText(videoBean.getDatas().get(0).getData().getStudyDescription());
            tvJudge.setText(videoBean.getDatas().get(0).getData().getDiagnosticImpression());
            if(type == 1)
            {
                if(videoBean.getDatas().get(0).getData().getRepetorViewUrl().length()>0) {
                    Intent intent = new Intent(this, Act_Webview.class);
                    intent.putExtra("url", videoBean.getDatas().get(0).getData().getRepetorViewUrl());
                    intent.putExtra("title", "报告详情");
                    startActivity(intent);
                }
                else {
                    ViewInject.toast("报告不存在");
                }
            }
            else {
                if(videoBean.getDatas().get(0).getData().getImageViewUrl().length()>0) {
                    Intent intent = new Intent(this, Act_X5Webview.class);
                    intent.putExtra("url", videoBean.getDatas().get(0).getData().getImageViewUrl());
                    intent.putExtra("title", "影像详情");
                    startActivity(intent);
                }
                else {
                    ViewInject.toast("影像不存在");
                }
            }
        }
        else {
            ViewInject.toast(videoBean.getMessage());
        }
    }
}
